// subPages/health/healthEducation/healthEducation.js
const app = getApp();
var util =  require('../../../utils/util.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type='content'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [{nav: '健康资料'},{nav: '健康讲堂'}],
    current: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    loadMoreView = that.selectComponent("#loadMoreView")
    that.setMainNav(that.data.current)
  },

  //选择导航栏
  switchNav(event){
    var that = this
    var cur = event.currentTarget.dataset.current;
    // console.log(cur)
    if (that.data.current == cur) {
      return false;
    } else {
      that.setMainNav(cur)
      that.setData({
        current: cur
      })
      that.getContentAll()
    }
  },

  //设置主导航
  setMainNav:function(cur){
    var typeUrl = ''
    switch (cur) {
      case 0:
        typeUrl = '/get_community_health_new_video_all'
        type = 'content'
        break;
      case 1:
        typeUrl = '/get_community_health_teacher_video'
        type = 'lecture'
        break;
    }
    this.setData({
      typeUrl: typeUrl
    })
  },

  //获取健康资料内容
  getContentAll:function(){
    //获取数据数据
    var that = this
    var data = []
    data.push(0)
    data.push(this.data.communityId)
    data = JSON.stringify(data)
    // console.log(data)
    util.getAjax(`${app.globalData.apiUrl}${that.data.typeUrl}`,that.getContent,{p: p,num: num,community_id: data},that.contentErr)
  },

  //获取健康资料内容
  getContent(res){
    // console.log(res)
    var that = this
    if(res.data.length != 0){
      var contentList = []
      for(var i in res.data.list){
        contentList.push(res.data.list[i])
      }
      that.setData({
        contentList: contentList
      })
    }else{
      that.setData({
        contentList: []
      })
    }
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取商品数据异常
  contentErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      switch (this.data.current) {
        case 0:
          text = '健康资料'
          break;
        case 1:
          text = '健康讲堂'
          break;
      }
      util.showModal('提示','获取'+ text +'失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getContentAll()
  },

  clickLoadMore: function(e) {
    this.getContentAll()
  },



  //内容详情
  contentBtn:function(e){
    if(e.currentTarget.dataset.type == 1){
      wx.navigateTo({
        url: '/subPages/health/article/article?id=' + e.currentTarget.dataset.id,
      })
    }
  },

  //播放结束
  viedoEnd:function(e){
    var that = this
    var type = that.data.current == 0?2:3
    util.getAjax(app.globalData.apiUrl +'/add_see',function(){
      //获取健康资料内容
      that.getContentAll()
    },{id: e.currentTarget.dataset.id,type: type})
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //社区id
    var that = this
    if(that.data.typeUrl){
      if(app.globalData.communityId){
        that.setData({
          communityId: app.globalData.communityId
        })
        that.getContentAll()
      }else{
        app.communityIdCallback = communityId => {
          if (communityId != '') {
            that.setData({
              communityId: communityId
            })
            that.getContentAll()
          }
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getContentAll()
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})